//
//  UserViewModel.swift
//  ShouChong
//
//  Created by yw on 2024/8/14.
//

import Foundation

struct UserModel:Identifiable,Codable {
    var id:Int = 0
    var name:String = ""
    var age:String = ""
    var uid:String = ""
    var vip:String = ""
    var image:String = ""
    var sex:String = ""
    var account:String = ""
    var password:String = ""
    var remark:String = ""
    var lat:String = ""
    var lng:String = ""
    var updateTime:String = ""
}


class UserViewModel:ObservableObject {
    @Published var model:UserModel = UserModel()
    
    init(getNow:Bool = true) {
        if getNow {
            getUserInfo()
        }
    }
    
    func getUserInfo() {
        YWNetworkTool.share().requestJson(url: "info/getNNUserInfo", parameters: nil) { res in
            DPrint(res)
            guard let results = res as? [String:Any],
                  let data = results["data"] as? [String:Any],
                  let jsonData = try? JSONSerialization.data(withJSONObject: data) else {
//                self.model = HomeModel()
                YWToast.show(text: "获取用户信息失败")
                return
            }
            do {
                let model = try JSONDecoder().decode(UserModel.self, from: jsonData)
                DPrint(model)
                self.model = model
            } catch {
                DPrint("error:\(error)")
                YWToast.show(text: "获取用户信息失败")

            }
        } responseFailBlock: { error, code in
            YWToast.show(text: "获取用户信息失败")
        }

    }
    
    func bindUserMsg(account:String,password:String,completion:@escaping ()->Void) {
        let sendData = [
            "account":account,
            "password":password
        ]
        YWNetworkTool.share().requestJson(url: "user/bindAccount", parameters: sendData) { res in
            DPrint(res)
            guard let results = res as? [String:Any],
                  let status = results["status"] as? Int else {
                YWToast.show(text: "绑定失败,请稍后再试")
                return
            }
            if status == 200 {
                YWToast.show(text: "绑定成功")
                completion()
            } else if status == 500 {
                YWToast.show(text: "该账号已被其他用户绑定，请更换一个账号")
            }else {
                YWToast.show(text: "绑定失败,请稍后再试")
            }
        } responseFailBlock: { error, code in
            YWToast.show(text: "绑定失败,请稍后再试")
        }

    }
    
    func removeAccount(completion:@escaping () -> Void) {
        YWNetworkTool.share().requestJson(url: "user/removeAccount", parameters: nil) { res in
            guard let results = res as? [String:Any],
                  let status = results["status"] as? Int,
                  let message = results["message"] as? String
                    else
            {
                YWToast.show(text: "操作失败,请稍后再试")
                return
            }
            if status == 200 {
                YWToast.show(text: "该用户已注销")
                completion()
            }else {
                YWToast.show(text: message)
            }
            
        } responseFailBlock: { error, code in
            YWToast.show(text: "操作失败,请稍后再试")
        }

    }
    
    func getActiveUsers(models:@escaping (_:[UserModel]) -> Void) {
        YWNetworkTool.share().requestJson(url: "user/getActiveUsers", parameters: nil) { res in
            DPrint(res)
            guard let results = res as? [String:Any],
                  let status = results["status"] as? Int,
                  let message = results["message"] as? String,
                  let data = results["data"] as? [[String:Any]],
                  let jsonData = try? JSONSerialization.data(withJSONObject: data)
                    else
            {
                YWToast.show(text: "获取L友列表失败")
                return
            }
            if status == 200 {
                do {
                    let model = try JSONDecoder().decode([UserModel].self, from: jsonData)
//                    DPrint(model)
                    models(model)
                } catch {
                    YWToast.show(text: "获取L友列表失败")
                }
                
            }else {
                YWToast.show(text: message)
            }
            
        } responseFailBlock: { error, code in
            YWToast.show(text: "获取L友列表失败")
        }
    }
    
    
    func setUserInfoVIP(isVIp:Bool) {
        let sendDict = [
            "isVip":isVIp ? "1":"0"
        ]
        YWNetworkTool.share().requestJson(url: "user/bindUserVIP", parameters: sendDict) { res in
            guard let results = res as? [String:Any]
//                  let status = results["status"] as? Int,
//                  let message = results["message"] as? String
                    else
            {return}
            DPrint(res)
        } responseFailBlock: { error, code in
            
        }
        
    }
    
 
    
    
    
}
